/**
 **@constructor {Segment}
 ** represent  data-seg='menu'
 **/
define(['static',
    './monitor/control',
    './monitor/table',
    './setting'
], function(
    CONST,
    ctl,
    table,
    setting
) {
    extend(Monitor, Segment); //继承 保持和 class-instance 分离语言的继承感觉。 放在最上面，因为
    function Monitor() {
        var _this = this;
        var _super = arguments.callee.parent;
        /**
         ** 初始化页面配置
         **/
        this.init = function() {
            this._DOM = $("[data-seg='monitor']"); //当前这个类 hold住的前端引用
            this.children = [ctl, table]; //获得 piece 实例的引用
            _super.init.call(_this);
            this.event();
        };
        this.event = function() {
            this._DOM.off('fold-table').on('fold-table', function(e) {
                table.foldTable(e.originalEvent.data.open);
            })
            this._DOM.off('adManageTable-result').on('adManageTable-result', function(e, result) {
                ctl.render(result);
            })
            this._DOM.off('adManageTable-sort').on('adManageTable-sort', function(e, sortParams) {
                var params = setting.getParams();
                if (setting.valicateParams(params)) {
                    _this.sortTable(sortParams);
                }
            })
            this._DOM.off('control-click').on('control-click', function(e) {
                _this.updateTable(_this.params);
            })

        }

        /**
         * { 更新表格数据 }
         *
         * @method     updateTable
         * @param      {<type>}  params  { description }
         */
        this.updateTable = function(params) {
            var controlParams = ctl.getParams();
            var settingParams = setting.getParams();
            console.info(this.params)
            this.params = $.extend(params, controlParams); //将查询缓存起来
            table.query(this.params);
        }

        this.sortTable = function(sortParams) {
            var controlParams = ctl.getParams();
            _this.params = $.extend(_this.params, controlParams); //将查询缓存起来
            _this.params.sort = sortParams.data;
            table.query(_this.params);
        }

        /**
         ** 处理用户交互事件，引起的dom更新
         **/
        this.update = function(msg) {
            switch (msg.type) {
                //              case "scroll":
                case "mousemove":
                case "click":
                case "mousewheel":
                    setTimeout(function() {
                        var w = $('.treable-body').width();
                        if ($('.fmp-monitor').length == 0) {
                            return;
                        }
                        var top = $('.fmp-monitor').offset().top;
                        console.info(top)
                        if (top <= 50) {
                            var treableTop = $('.ndp-treable-wrapper').offset().top;
                            if (treableTop < 0) {
                                _this._DOM.find('.treable-header').css({
                                    'z-index': '399',
                                    'position': 'absolute',
                                    'top': (treableTop * -1 + 49) + 'px',
                                    'border': '1px solid #ccc',
                                });
                            }
                        } else {
                            _this._DOM.find('.treable-header').css({
                                'z-index': '',
                                'position': '',
                                'top': '',
                                'border': '0',
                            });
                        }
                    }, 500);

                    break;
                case "shrink_complete":
                    if (this._DOM.hasClass("fixed")) {
                        this._DOM.addClass("shrink");
                    }
                    break;
                case "expand_complete":
                    if (this._DOM.hasClass("fixed")) {
                        this._DOM.removeClass("shrink");
                    }
                    break;
            }
            _super.update.call(this, msg);
        }

    }
    return new Monitor();
});
